<template>
  <div class="container">
    <div class="gir-content">
      <div class="top">
        <img src="../assets/logo.png" alt="">
      </div>
      <div class="deng">
        <el-form label-width="80px" :rules="rules" :model="form" ref="form">
          <el-form-item label="账号 :" prop="username">
            <el-input v-model="form.username" v-focus>
            </el-input>
          </el-form-item>
          <el-form-item label="密码 :" prop="password">
            <el-input type="password" v-model="form.password"></el-input>
          </el-form-item>
          <el-form-item class="btns">
            <el-button type="primary" @click="submitForm()">提交</el-button>
            <el-button @click="resetForm()">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
    export default {
        name: 'login',
      data:function(){
          return {
            form:{
              password:'123456',
              username:'admin'
            },
            /*监听格式*/
            rules:{
              username:[
                {required: true, message: '请输入账号', trigger: 'blur' },
                { min: 4, max: 10, message: '最少要4位数', trigger: 'blur' }
              ],
              password:[
                {required: true, message: '请输入密码', trigger: 'blur' },
                { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
              ]
            }
          }
      },
      methods:{
          //重置
        resetForm:function() {
          this.$refs.form.resetFields();
        },
        //提交
        submitForm:function(){
          this.$refs.form.validate(async (valid) => {
            if(!valid){
              return;
            }
            let { data:http } = await this.$http.post('login',this.form);
            if(http.meta.status!=200){
              this.$message.error('登录失败');
              return false;
            }
            //this.$message.success('成功登录了');
            //设置身份验证（请求头）
            window.sessionStorage.setItem('token',http.data.token);
            this.$router.push('home');
         })
        }
      },
      directives:{
          focus:{
            inserted:function(el){
              el.children[0].focus();
            }
          }
      }
    }
</script>
<style lang="less" scoped>
  .container{
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: #2b4b6b;
  }
  .container .gir-content{
    position: absolute;
    top: 50%;
    left: 50%;
    margin: -150px -225px;
    width: 450px;
    height: 300px;
    -webkit-border-radius: 3px;
    -moz-border-radius: 3px;
    border-radius: 10px;
    background-color: #ffffff;
  }
  .container .gir-content .top {
    padding: 10px;
    height: 130px;
    width: 130px;
    border-radius: 50%;
    background-color: #fff;
    border: 1px solid darkgrey;
    box-shadow: 0 0 10px #ddd;
    transform: translate(100%,-50%);
    img {
      height: 100%;
      border-radius: 50%;
      background-color: #ffffd5;
      border: 1px solid darkgrey;
    }
  }
  .deng{
    width: 90%;
    transform: translate(0px,-45px);
  }
  .btns{
    transform: translate(45%, 0px);
  }

</style>
